package net.suncaper.hotel.service.impl;

import net.suncaper.hotel.common.utils.PageInfo;
import net.suncaper.hotel.mapper.IUserMapper;
import net.suncaper.hotel.mapper.impl.UserMapperImpl;
import net.suncaper.hotel.model.domain.User;
import net.suncaper.hotel.model.dto.UserDTO;
import net.suncaper.hotel.model.dto.UserLoginDTO;
import net.suncaper.hotel.model.vo.UserVO;
import net.suncaper.hotel.service.IUserService;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户服务实现类
 */
public class UserServiceImpl implements IUserService {
    private IUserMapper userMapper = new UserMapperImpl();

    @Override
    public UserVO login(UserLoginDTO loginDTO) {
        if (loginDTO == null || loginDTO.getUsername() == null || loginDTO.getPassword() == null) {
            return null;
        }
        
        User user = userMapper.findByUsername(loginDTO.getUsername());
        if (user == null || !user.getPassword().equals(loginDTO.getPassword())) {
            return null;
        }
        
        if (user.getStatus() != 1) {
            return null; // 用户被禁用
        }
        
        return convertToVO(user);
    }

    @Override
    public UserVO getUserById(Integer id) {
        if (id == null) {
            return null;
        }
        User user = userMapper.findById(id);
        return convertToVO(user);
    }

    @Override
    public PageInfo<UserVO> getUserList(int pageNum, int pageSize) {
        PageInfo<UserVO> pageInfo = new PageInfo<>(pageNum, pageSize);
        
        // 获取总数
        long total = userMapper.count();
        pageInfo.setTotal(total);
        
        // 获取分页数据
        List<User> users = userMapper.findByPage(pageInfo.getStartRow(), pageSize);
        List<UserVO> voList = new ArrayList<>();
        if (users != null) {
            for (User user : users) {
                voList.add(convertToVO(user));
            }
        }
        pageInfo.setList(voList);
        
        return pageInfo;
    }

    @Override
    public boolean createUser(UserDTO userDTO) {
        if (userDTO == null || userDTO.getUsername() == null || userDTO.getPassword() == null) {
            return false;
        }
        
        // 检查用户名是否已存在
        User existUser = userMapper.findByUsername(userDTO.getUsername());
        if (existUser != null) {
            return false;
        }
        
        User user = new User();
        user.setUsername(userDTO.getUsername());
        user.setPassword(userDTO.getPassword());
        user.setRealName(userDTO.getRealName());
        user.setPhone(userDTO.getPhone());
        user.setEmail(userDTO.getEmail());
        user.setRole(userDTO.getRole() != null ? userDTO.getRole() : "EMPLOYEE");
        user.setStatus(userDTO.getStatus() != null ? userDTO.getStatus() : 1);
        
        return userMapper.insert(user) > 0;
    }

    @Override
    public boolean updateUser(UserDTO userDTO) {
        if (userDTO == null || userDTO.getId() == null) {
            return false;
        }
        
        User user = userMapper.findById(userDTO.getId());
        if (user == null) {
            return false;
        }
        
        user.setRealName(userDTO.getRealName());
        user.setPhone(userDTO.getPhone());
        user.setEmail(userDTO.getEmail());
        user.setRole(userDTO.getRole());
        user.setStatus(userDTO.getStatus());
        
        return userMapper.update(user) > 0;
    }

    @Override
    public boolean deleteUser(Integer id) {
        if (id == null) {
            return false;
        }
        return userMapper.delete(id) > 0;
    }

    @Override
    public boolean updatePassword(Integer id, String oldPassword, String newPassword) {
        if (id == null || oldPassword == null || newPassword == null) {
            return false;
        }
        
        User user = userMapper.findById(id);
        if (user == null || !user.getPassword().equals(oldPassword)) {
            return false;
        }
        
        user.setPassword(newPassword);
        return userMapper.update(user) > 0;
    }

    private UserVO convertToVO(User user) {
        if (user == null) {
            return null;
        }
        
        UserVO vo = new UserVO();
        vo.setId(user.getId());
        vo.setUsername(user.getUsername());
        vo.setRealName(user.getRealName());
        vo.setPhone(user.getPhone());
        vo.setEmail(user.getEmail());
        vo.setRole(user.getRole());
        vo.setStatus(user.getStatus());
        vo.setCreateTime(user.getCreateTime());
        vo.setUpdateTime(user.getUpdateTime());
        
        return vo;
    }
}